﻿using System.Collections.Generic;
using System.Data.SqlServerCe;

namespace WindowsFormsApplication2
{
    public abstract class Repository<T> : WindowsFormsApplication2.IRepository<T>
    {
        string _connectionString;
        protected Repository(IDbSettings settings)
        {
            _connectionString = settings.ConnectionString;
        }

        public abstract T Map(SqlCeDataReader reader);

        public List<T> Read(string commandText )
        {
            List<T> items = new List<T>();

            SqlCeConnection connection = new SqlCeConnection();
            connection.ConnectionString = _connectionString; 
            connection.Open();

            SqlCeCommand command = new SqlCeCommand();
            command.Connection = connection;
            command.CommandText = commandText;

            SqlCeDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                items.Add(Map(reader));
            }

            connection.Close();

            return items;
        }
    }
}
